<template>
  <component class="no-underline c-inherit" :is="linkType" v-bind="linkProps(to)">
    <slot />
  </component>
</template>

<script setup lang="ts" name="AppLink" inheritAttrs="false">
import { isExternal } from '@/utils'

const props = defineProps({
  to: {
    type: Object,
    required: true
  }
})

const isExternalLink = computed(() => {
  return isExternal(props.to.path || '')
})

const linkType = computed(() => (isExternalLink.value ? 'a' : 'router-link'))

const linkProps = (to: any) => {
  if (isExternalLink.value) {
    return {
      href: to,
      target: '_blank',
      rel: 'noopener'
    }
  }
  return { to: to }
}
</script>
